package com.gqz.equip.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.app.entity.TEnInterfaceCodeEntity;
import com.gqz.equip.mapper.TEnInterfaceCodeMapper;
import com.gqz.equip.mapstruct.TEnInterfaceCodeMapstruct;
import com.gqz.equip.service.ITEnInterfaceCodeService;
import com.gqz.equip.vo.TEnInterfaceCodeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
@CacheConfig(cacheNames = "code")
public class TEnInterfaceCodeServiceImpl extends ServiceImpl<TEnInterfaceCodeMapper, TEnInterfaceCodeEntity> implements ITEnInterfaceCodeService {

    @Autowired
    private TEnInterfaceCodeMapstruct mapstruct;

    @Override
    @Cacheable(value = "code", key = "#id", unless = "#result == null")
    public List<TEnInterfaceCodeVo> getById(String id) {
        MPJLambdaWrapper<TEnInterfaceCodeEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(TEnInterfaceCodeEntity::getId, id);
        queryWrapper.selectAll(TEnInterfaceCodeEntity.class);
        List<TEnInterfaceCodeEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    @Cacheable(value = "code", key = "#requestForm.entity.code", unless = "#result == null")
    public Page<TEnInterfaceCodeVo> page(RequestForm<TEnInterfaceCodeVo> requestForm) {
        TEnInterfaceCodeEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnInterfaceCodeEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(TEnInterfaceCodeEntity.class);
        Page<TEnInterfaceCodeEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<TEnInterfaceCodeEntity> iPage = baseMapper.selectJoinPage(page, TEnInterfaceCodeEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<TEnInterfaceCodeVo> requestForm) {
        TEnInterfaceCodeEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnInterfaceCodeEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.select(TEnInterfaceCodeEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    @CachePut(value = "code", key = "#vos.hashCode()")
    public int insert(List<TEnInterfaceCodeVo> vos) {
        List<TEnInterfaceCodeEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TEnInterfaceCodeEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    @CacheEvict(value = "code", key = "#vos.hashCode()")
    public int update(List<TEnInterfaceCodeVo> vos) {
        List<TEnInterfaceCodeEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TEnInterfaceCodeEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @Override
    @Cacheable(value = "code", key = "#code", unless = "#result == null")
    public List<TEnInterfaceCodeVo> queryDictData(String code) {
        MPJLambdaWrapper<TEnInterfaceCodeEntity> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(TEnInterfaceCodeEntity.class);
        queryWrapper.eq(TEnInterfaceCodeEntity::getIsactive, 1);
        if (!"".equals(code)) {
            queryWrapper.eq(TEnInterfaceCodeEntity::getType, code);
        } else {
            return new ArrayList<>();
        }

        queryWrapper.orderByAsc(TEnInterfaceCodeEntity::getOrderNo);

        List<TEnInterfaceCodeEntity> codeEntityList = baseMapper.selectList(queryWrapper);

        return mapstruct.toVos(codeEntityList);
    }

    @Override
    @Cacheable(value = "code", key = "#code", unless = "#result == null")
    public List<TEnInterfaceCodeVo> queryDictBm(String code) {
        MPJLambdaWrapper<TEnInterfaceCodeEntity> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(TEnInterfaceCodeEntity.class);
        queryWrapper.eq(TEnInterfaceCodeEntity::getIsactive, 1);
        queryWrapper.like(TEnInterfaceCodeEntity::getType, code);
        //queryWrapper.eq(TEnInterfaceCodeEntity::getValue, value);
        queryWrapper.orderByAsc(TEnInterfaceCodeEntity::getOrderNo);

        List<TEnInterfaceCodeEntity> codeEntityList = baseMapper.selectList(queryWrapper);

        return mapstruct.toVos(codeEntityList);
    }


}
